<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class ActivityController extends AlumniController{
    
    
    
    public function actionView(){
        

        
      if(Yii::app()->request->isAjaxRequest){

         $page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
         $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;  
         $id = isset($_POST['id']) ?$_POST['id'] : '';  
         $typeid = isset($_POST['type_id']) ?$_POST['type_id']: '';  
         $start_time=isset($_POST['start_time'])?strtotime($_POST['start_time']):'';
         $end_time=isset($_POST['end_time'])?strtotime($_POST['end_time']):'';

         $offset = ($page-1)*$rows;  

         $result = array();  

         $where = " where a.id like '$id%' and t.name like '$typeid%'";  
         
         if($start_time){
             $where .=" and a.start_time >={$start_time} ";
         }
         if($end_time){
             $where .=" and a.end_time < {$end_time} ";
         }
         
         //$rs = mysql_query("select count(*) from item where " . $where);  
         //$row = mysql_fetch_row($rs);  
          

         $sql ='SELECT a.id,a.title,a.start_time,a.end_time,t.name as type_id FROM xyh_activity as a 
               LEFT JOIN xyh_type as t on a.type_id = t.id';
         $sql .=$where;
         $dataProvider = Yii::app()->db->createCommand($sql)->queryAll();
         $result["total"] = count($dataProvider);
         
        
         $sql =$sql."limit $offset,$rows";
         $dataProvider = Yii::app()->db->createCommand($sql)->queryAll();
          

         
//         foreach ($dataProvider as $key => $value)
//         {
//             if($value['start_time']){
//                 $dataProvider[$key]['start_time']=date('Y-m-d',$value['start_time']);
//             }
//             if($value['end_time']){
//                 $dataProvider[$key]['end_time']=date('Y-m-d',$value['end_time']);
//             }
//         }
                 
       $result["rows"] = $dataProvider;  
  
        //p_e($dataProvider);
        echo json_encode($dataProvider);  
          
          
          
          
      }else{
          $this->render('view');
      }
       
        
    }
    
    public function actionAdd(){
        
        $Activity = new Activity();  
        $ActivityLinkSignup = new ActivityLinkSignup();
        $ActivityLinkVoteA = new ActivityLinkVoteA();
        $ActivityLinkVoteQ = new ActivityLinkVoteQ();
        
        
       $Activity->unsetAttributes();
       $ActivityLinkSignup->unsetAttributes();
       $ActivityLinkVoteA->unsetAttributes();
       $ActivityLinkVoteQ->unsetAttributes();

       if(isset($_GET['Activity'])){
           
            $Store->attributes=$_GET['Activity'];
        }


        if((isset($_POST['ajax']) && $_POST['ajax']==='active-form'))
        {
              //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($Activity,$ActivityLinkSignup));  
            Yii::app()->end();
  
        }
        if((isset($_POST['ajax']) && $_POST['ajax']==='vote-form'))
        {
              //两个model ajax验证        
            //p_e($_POST);           
            echo CActiveForm::validate(array($Activity,$ActivityLinkSignup));  
            Yii::app()->end();
  
        }
        
        // collect user input data
        if(isset($_POST['Activity']) && isset($_POST['Activity']))
        {	
              
           $data = $this->Data_Pro($_POST);
           
           $Activity->attributes = $data['Activity'];
            //p_e($data);

            $transaction  =Yii::app()->db->beginTransaction();
            
           if($Activity->save()){
               
               $Activity_id =Yii::app()->db->getLastInsertID();
               $data['ActivityLinkVoteQ']['activity_id']=$Activity_id;
               
               if($data['Activity']['type_id'] ==102){
                    foreach ($data['ActivityLinkVoteQ']['q_content'] as $k=>$v)
                    {                   
                             $data['ActivityLinkVoteQ'][$k]['activity_id']= $Activity_id;
                             $data['ActivityLinkVoteQ'][$k]['q_content'] =$v;
                             $data['ActivityLinkVoteQ'][$k]['type_id'] = $data['ActivityLinkVoteQ']['type_id'];
                             $data['ActivityLinkVoteQ'][$k]['sort'] =($k+1);

                             $ActivityLinkVoteQ->attributes = $data['ActivityLinkVoteQ'][$k];
                              //p_e($ActivityLinkVoteQ->attributes);
                             if ($ActivityLinkVoteQ->save(false)){
                                 $ActivityLinkVoteQ = new ActivityLinkVoteQ();
                                 $vote_id = Yii::app()->db->getLastInsertID();
                                  foreach ($data['ActivityLinkVoteA']['a_content'][$k] as $k=>$v)
                                {                   
                                         $data['ActivityLinkVoteA'][$k]['activity_id']= $Activity_id;
                                         $data['ActivityLinkVoteA'][$k]['vote_q_id']= $vote_id;
                                         $data['ActivityLinkVoteA'][$k]['a_content'] =$v;
                                         
                                         $data['ActivityLinkVoteA'][$k]['sort'] =($k+1);

                                         $ActivityLinkVoteA->attributes = $data['ActivityLinkVoteA'][$k];

                                          //p_e($ActivityLinkVoteA->attributes);

                                         if ($ActivityLinkVoteA->save(false)){

                                             $ActivityLinkVoteA = new $ActivityLinkVoteA();
                                         }
                                         else{
                                              var_dump($ActivityLinkVoteA->getErrors()) ;exit();
                                             $transaction->rollback();
                                             Yii::app()->user->setFlash('addform',"答案添加失败");
                                             barek;
                                         }
                                 }
                                 
                                 
                             }
                             else{
                                 
                                 $transaction->rollback();
                                  var_dump($ActivityLinkVoteQ->getErrors()) ;exit();
                                 Yii::app()->user->setFlash('addform',"问题添加失败");
                                 barek;
                             }
                     }
                     
                      $transaction->commit();
                      Yii::app()->user->setFlash('addform', "添加活动成功！");
                      $this->refresh();
               }
                if($data['Activity']['type_id'] ==101)
               {
                        $data['ActivityLinkSignup']['activity_id'] = $Activity_id;
                        
                        $ActivityLinkSignup->attributes = $data['ActivityLinkSignup'];
                       // p_e($ActivityLinkSignup->attributes);
                        if($ActivityLinkSignup->save()){
                             $transaction->commit();
                             Yii::app()->user->setFlash('addform', "添加活动成功！");
                             $this->refresh();
                        }else{
                                 
                                 $transaction->rollback();
                                 var_dump($ActivityLinkSignup->getErrors()) ;exit();
                                 Yii::app()->user->setFlash('addform',"报名活动失败");
                                 barek;
                        } 
               }
                
               
           }else{
                var_dump($Activity->getErrors()) ;exit();
                $transaction->rollback();
                Yii::app()->user->setFlash('addform',"活动添加失败");
                barek;
           }
           
           
        }
        
        
            
            $this->render('add',array(
                
               'Activity'=>$Activity,
               'ActivityLinkSignup'=>$ActivityLinkSignup,
               'ActivityLinkVoteA'=>$ActivityLinkVoteA,
               'ActivityLinkVoteQ'=>$ActivityLinkVoteQ,
                ));
    }
    
    public function actionAjaxAdd(){
        
        
        $type_id= $_POST['type_id'];
        $title = $_POST['title'];
        

        $sql = "insert into xyh_activity(type_id,title) values('$type_id','$title')";
      
        $result = Yii::app()->db->createCommand($sql)->execute();
        
        
        if ($result){
                echo json_encode(array('success'=>true));
        } else {
                echo json_encode(array('msg'=>'Some errors occured.'));
        }
       
            
    }
    
    function actionEdit(){
        
        $id= $_GET['id'];
        $type_id= $_POST['type_id'];
        $title = $_POST['title'];
  
        $sql = "UPDATE xyh_activity SET type_id={$type_id},title={$title} Where id={$id}";
      
        $result = Yii::app()->db->createCommand($sql)->execute();

        if ($result){
                echo json_encode(array('success'=>true));
        } else {
                echo json_encode(array('msg'=>'Some errors occured.'));
        }
        
    }
    
    
    
    
    
    
    
    
    
    
        public function actionView1(){
            
            $active = new Activity();
            
            $active->unsetAttributes();
            
            
            $this->render('view1',array(
                
               'active'=>$active
                
                ));
            
        }
        
        
        public function Data_Pro($post)
    {   
        //相同数据
        $post['Activity']['alumni_id'] = isset($_SESSION['alumni_id'])?$_SESSION['alumni_id']:0;
        $post['Activity']['cat_id'] = 1001;
        
        
        $post['Activity']['start_time'] = strtotime($post['Activity']['start_time']);
        $post['Activity']['end_time'] = strtotime($post['Activity']['end_time']);
        $post['Activity']['status'] = 1; 
        $post['Activity']['ctime'] = time(); 
        
        
        if($post['Activity']['type_id'] ==101){//报名活动
            
             if(isset($post['ActivityLinkSignup']['is_need_lottery'])){
                $post['ActivityLinkSignup']['lottery_start_time'] = strtotime($post['ActivityLinkSignup']['lottery_start_time']);
                $post['ActivityLinkSignup']['lottery_end_time'] = strtotime($post['ActivityLinkSignup']['lottery_end_time']);
             }
             
             if(isset($post['ActivityLinkSignup']['is_need_signup'])){
                $post['ActivityLinkSignup']['signup_start_time'] = strtotime($post['ActivityLinkSignup']['signup_start_time']);
                $post['ActivityLinkSignup']['signup_end_time'] = strtotime($post['ActivityLinkSignup']['signup_end_time']);
             }
        }

    return $post;
      
    }
    
    
}